<?php
/**
 * Created by IntelliJ IDEA.
 * User: ahwwl
 * Date: 18-10-18
 * Time: 下午6:16
 */

namespace commen;


use think\Log;
use think\Request;

class xinditu
{
    private static $url = "http://llcz.xinditu.com//";
    private static $username = "17006830684";
    private static $key = "f951828f6759051aa89f376e4131c805";

    static function get_balance()
    {
        return self::get_curl("CIV2/getBalance", ["username"=>self::$username]);
    }

    static function get_order($order_no)
    {
        return self::get_curl("CIV2/QueryOrder", ["username"=>self::$username,"ext_id"=>$order_no]);
    }
    static function recharge($order_no,$card_no,$amount)
    {
        return self::get_curl("CIV2/PetroCharge", ["username"=>self::$username,"ext_id"=>$order_no,"dest_no"=>$card_no,"per_value"=>$amount]);
    }
    static function get_curl($action, $params=[])
    {
        $params["username"] = self::$username;
        $params["format"] = "json";
        $headers = [];
        $url = self::$url . $action;
        ksort($params);
        $sign_str = "";
        foreach($params as $key=>$val)
        {
            if(!empty($val))
                $sign_str.=$key.$val;
        }
        $sign_str.= self::$key;
        $params["sign"] = md5($sign_str);
        $query = http_build_query($params);
        $url.="?".$query;
        $info = ["error" => 1, "error_info" => "", "data" => [],"raw_post_data"=>""];
        Log::log(json_encode([$url, $headers]));
        $result= self::curl_url($url, $headers);
        Log::log(json_encode($result));
        $info["raw_post_data"] = json_encode($result);
        if ($result["code"] == 200) {
            $body = json_decode($result["body"], true);
            if(empty($body))
                $info["error_info"] = $result["error_info"];
            else{
                $info["error"] = 0;
                $info["data"] = $body;
            }

        } else {
            $info["error_info"] = $result["error_info"];
        }
        return $info;
    }

    static function curl_url($url, $headers=[], $body=[])
    {
        $result = ["code" => 1, "body" => "", "error_info" => ""];
        try {
            $ch = curl_init();
            //设置选项，包括URL
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            if (!empty($headers))
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_POST, 1);
            if (!empty($body)) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
            }
            //执行并获取HTML文档内容
            $output = curl_exec($ch);
//            print_r($output);
            if ($output === false) {
                $result["error_info"] = curl_errno($ch);
            }
            $result["code"] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            $result["body"] = $output;
            //释放curl句柄
            curl_close($ch);
        } catch (\Exception $e) {
            $result["error_info"] = $e->getMessage();
        }
        return $result;
    }

}